/*
 * Copyright (c) Created by T C L on 2019.7.8.
 */

package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;

import org.json.JSONArray;
import org.json.JSONException;

import cn.mumzone.basic.adapter.rv.CommonAdapter;
import cn.mumzone.basic.adapter.rv.ViewHolder;
import cn.mumzone.basic.util.IsFastClickUtil;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.DepositGoodsEntity;
import cn.mumzone.pos.ui.dialog.DepositGoodsReceiveDialogFragment;
import cn.mumzone.pos.ui.dialog.DepositGoodsReceiveRecordDialogFragment;

public class ADA_DepositGoods extends CommonAdapter<DepositGoodsEntity> {

    public ADA_DepositGoods(Context context) {
        super(context);
    }

    @Override
    protected int itemLayoutId() {
        return R.layout.item_deposit_good;
    }

    @Override
    protected void convert(ViewHolder holder, final DepositGoodsEntity depositEntity, int position) {

        holder.setText(R.id.tv_good_name, depositEntity.getGoodsName());

        StringBuilder goodsSpec = new StringBuilder();
        goodsSpec.append("规格：");
        try {
            if (!TextUtils.isEmpty(depositEntity.getGoodsSpec())) {
                JSONArray jsonArray = new JSONArray(depositEntity.getGoodsSpec());
                for (int i = 0; i < jsonArray.length(); i++) {
                    goodsSpec.append(jsonArray.getJSONObject(i).optString("value")).append("，");
                }
                if (goodsSpec.lastIndexOf("，") != -1) {
                    goodsSpec.deleteCharAt(goodsSpec.lastIndexOf("，"));
                } else {
                    goodsSpec.append(depositEntity.getUnitName());
                }
                holder.setText(R.id.tv_good_spec, goodsSpec.toString());
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


        if (depositEntity.getConsignNum() - depositEntity.getSendNum() == 0) {
            holder.setVisible(R.id.tv_remain_count, false);
            holder.setVisible(R.id.tv_receive_immediately, false);
            holder.setVisible(R.id.iv_receive_finished, true);
        } else {
            holder.setVisible(R.id.tv_remain_count, true);
            holder.setVisible(R.id.tv_receive_immediately, true);
            holder.setVisible(R.id.iv_receive_finished, false);
            holder.setText(R.id.tv_remain_count,
                    "剩余寄存：" + (depositEntity.getConsignNum() - depositEntity.getSendNum()));
        }

        final Bundle bundle = new Bundle();
        bundle.putSerializable("depositEntity", depositEntity);
        holder.setOnClickListener(R.id.root_layout, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(IsFastClickUtil.isFastClick(v)){
                    return;
                }
                DepositGoodsReceiveRecordDialogFragment depositGoodsReceiveRecordDialogFragment =
                        new DepositGoodsReceiveRecordDialogFragment();
                depositGoodsReceiveRecordDialogFragment.setArguments(bundle);
                depositGoodsReceiveRecordDialogFragment.show(((AppCompatActivity) mContext).getSupportFragmentManager(), "depositGoodsReceiveRecord");
            }
        });

        holder.setOnClickListener(R.id.tv_receive_immediately, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(IsFastClickUtil.isFastClick(v)){
                    return;
                }
                DepositGoodsReceiveDialogFragment depositGoodsReceiveDialogFragment =
                        new DepositGoodsReceiveDialogFragment();
                depositGoodsReceiveDialogFragment.setArguments(bundle);
                depositGoodsReceiveDialogFragment.show(((AppCompatActivity) mContext).getSupportFragmentManager(),
                        "depositGoodsReceive");
            }
        });


    }
}
